<template>
	<el-dialog title="新增" :visible.sync="dialogFormVisible" :before-close="handleDialogClose" width="60%">
		<el-form :model="form" :rules="rules" ref="form" v-if="cleardata">
			<el-form-item label="标题" :label-width="formLabelWidth" prop="messTitle">
				<el-input v-model="form.messTitle" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="资讯内容" :label-width="formLabelWidth" prop="messContent">
				<div ref="editor"></div>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button @click="cleardata">取 消</el-button>
			<el-button type="primary" @click="submitForm('form')">确 定</el-button>
		</div>
	</el-dialog>
</template>

<script>
	import znMessApi from "@/api/znMessage.js"
	import Editor from "wangeditor"
	export default {
		data() {
			return {
				editor: null,
				dialogFormVisible: false,
				form: {
					messTitle: "",
					messContent: "",
				},
				rules: {
					messTitle: [{
						required: true,
						message: "请输入标题",
						trigger: 'blur'
					}]
				},
				formLabelWidth: "120px"
			};
		},
		methods: {
			async addZnMessage() {
				let content = this.editor.txt.html();
				const res = await znMessApi.addZnMessage("/znMessage/addZnMessage", {
					messTitle: this.form.messTitle,
					messContent: this.form.messContent = content,
				});
				if (res.code == 200) {
					this.dialogFormVisible = false;
					this.$message.success(res.msg);
					this.$parent.SearchZnMessageAll();
				} else {
					this.$message.error(res.msg);
				}
			},
			createEditor() {
				this.$nextTick(() => {
					this.editor = new Editor(this.$refs.editor)
					this.editor.config.uploadImgServer = "api/file/editUpload/message"
					this.editor.config.uploadFileName = "file"
					this.editor.config.uploadVideoServer = 'api/file/editUpload/message'
					this.editor.config.uploadVideoName = "file"
					this.editor.create()
				})
			},
			cleardata() {
				this.dialogFormVisible = false;
				this.$refs.form.resetFields();
				this.editor.destroy();
				this.editor = null;
			},
			handleDialogClose() {
				this.dialogFormVisible = false;
				this.$refs.form.resetFields();
				this.editor.destroy();
				this.editor = null;
			},
			submitForm(form) {
				this.$refs[form].validate((valid) => {
					if (valid) {
						this.addZnMessage();
						this.$refs.form.resetFields();
						this.editor.destroy();
						this.editor = null;
					} else {
						return false;
					}
				});
			},
		},
		created() {}

	};
</script>

<style lang="scss" scoped>

</style>